﻿using SQLRunnerLib.Project;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Collections.Generic;

namespace SQLUnitTests
{
    
    
    /// <summary>
    ///This is a test class for SQLProjectTest and is intended
    ///to contain all SQLProjectTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SQLProjectTest
        :TestBase
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for FilesInProject with only one folder and subfolders are not included.
        ///</summary>
        [TestMethod()]
        public void FilesInProjectNotIncludeSubFoldersTest()
        {
            SQLProject project = new SQLProject();
            project.Extensions.Add("sql");
            Folder folder = new Folder
            {
                Path = Path.Combine(System.Environment.CurrentDirectory, "basicscripts"),
                IncludeAllFiles = true,
                IncludeAllSubFolders = false
            };
            project.Folders.Add(folder);

            List<FileInfo> files = project.FilesInProject();
            Assert.AreEqual(2, files.Count);
        }

        /// <summary>
        ///A test for FilesInProject with only one folder and subfolders are included.
        ///</summary>
        ///<remarks>
        ///This test passes but I think the way IncludeSubFolders works is incorrect.  See work item 12577
        /// </remarks>
        [TestMethod()]
        public void FilesInProjectIncludeSubFoldersTest()
        {
            SQLProject project = new SQLProject();
            project.Extensions.Add("sql");
            Folder folder = new Folder
            {
                Path = Path.Combine(System.Environment.CurrentDirectory, "basicscripts"),
                IncludeAllFiles = true,
                IncludeAllSubFolders = true
            };
            Folder subfolder = new Folder
            {
                Path = Path.Combine(System.Environment.CurrentDirectory, "basicscripts\\subfolder1"),
                IncludeAllFiles = true,
                IncludeAllSubFolders = false
            };
            folder.Folders.Add(subfolder);
            project.Folders.Add(folder);

            List<FileInfo> files = project.FilesInProject();
            Assert.AreEqual(3, files.Count);
            Assert.AreEqual("0010 BasicScript.sql", files[0].Name);
            Assert.AreEqual("0020 BasicScript.sql", files[1].Name);
            Assert.AreEqual("0010 Insert Data.sql", files[2].Name);
        }

        /// <summary>
        ///A test for FilesInProject with only one folder and all files are included.
        ///</summary>
        [TestMethod()]
        public void FilesInProjectDontIncludeAllFilesTest()
        {
            SQLProject project = new SQLProject();
            project.Extensions.Add("sql");
            Folder folder = new Folder
            {
                Path = Path.Combine(System.Environment.CurrentDirectory, "basicscripts"),
                IncludeAllFiles = false,
                IncludeAllSubFolders = true
            };
            folder.Files.Add("somefile.sql");
            project.Folders.Add(folder);

            List<FileInfo> files = project.FilesInProject();
            Assert.AreEqual(1, files.Count);
            Assert.AreEqual("somefile.sql", files[0].Name);
        }


    }
}
